<?php

class Loader extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->load->view('login/login');
    }
    
    public function login() {
        $login = new stdClass();
        $login->username = $this->input->post('username');
        $login->password = $this->input->post('password');
        $this->load->model('player_model');
        $player = $this->player_model->login($login);
        if($player != null){
            $this->session->set_userdata('player', $player);
            $this->characters();
        } else {
            $this->index();
        }
    }
    
    public function load() {
        $data['game_screen'] = $this->load->view('game_screen/game_screen', null, TRUE);
        $this->load->view('main_game', $data);
    }
    
    public function characters() {
        $this->load->model('character_model');
        $data['characters'] = $this->character_model->getAllByPlayer($this->session->userdata('player')->id);
        $this->load->view('login/character_selection', $data);
    }
    
    public function selectCharacter() {
        $this->load->model('character_model');
        $characters = $this->character_model->getAllByPlayer($this->session->userdata('player')->id);
        $selectedCharacterId = $this->input->post('selected_character');
        $selectedCharacter = $this->character_model->getById($selectedCharacterId);
        $check = false;
        foreach ($characters as $character) {
            if ($character->id == $selectedCharacterId) {
                $check = true;
            }
        }
        
        if ($check) {
            $this->session->set_userdata('character', $selectedCharacter);
            $this->load();
        } else {
            $this->index();
        }
    }

}

?>
